<!--
 * @author  dumeng 375
-->
<template>
  <div class="works-preview">
    <el-dialog
      v-el-drag-dialog
      v-loading="previewLoading"
      :close-on-click-modal="false"
      :visible.sync="state"
      title="预览"
      width="450px"
      center
      :before-close="handleCancel"
    >
      <div class="preview-container">
        <div class="content" :class="{'b0': detail.commentState === 0 || detail.commentState === 1}">
          <div v-if="detail.worksName" class="title">
            <span>{{ detail.worksName }}</span>
          </div>
          <div v-if="detail.authorRealName" class="time">
            作者：{{ detail.authorRealName }}
          </div>
          <!--          <div class="checkInfo">-->
          <!--            <div class="row">-->
          <!--              <div class="key">作品编号</div>-->
          <!--              <div class="val">{{ detail.worksNumber }}</div>-->
          <!--            </div>-->
          <!--            <div class="row">-->
          <!--              <div class="key">提交时间</div>-->
          <!--              <div class="val">{{ detail.submitTime }}</div>-->
          <!--            </div>-->
          <!--            <div v-if="detail.auditResult === 'AUDITRESULT02' || detail.auditResult === 'AUDITRESULT03'" class="row">-->
          <!--              <div class="key">审核时间</div>-->
          <!--              <div class="val">{{ detail.auditTime }}</div>-->
          <!--            </div>-->
          <!--            <div class="row">-->
          <!--              <div class="key">审核状态</div>-->
          <!--              <div v-if="detail.auditResult === 'AUDITRESULT03'" class="val wtg">未通过</div>-->
          <!--              <div v-if="detail.auditResult === 'AUDITRESULT02'" class="val ytg">已通过</div>-->
          <!--              <div v-if="detail.auditResult === 'AUDITRESULT01'" class="val shz">审核中</div>-->
          <!--            </div>-->
          <!--            <div v-if="detail.auditResult === 'AUDITRESULT03'" class="row">-->
          <!--              <div class="key">未通过原因</div>-->
          <!--              <div class="val reason">{{ detail.auditRemark }}</div>-->
          <!--            </div>-->
          <!--            <div v-if="detail.winningName" class="row">-->
          <!--              <div class="key">获奖名称</div>-->
          <!--              <div class="val wtg">{{ detail.winningName }}</div>-->
          <!--            </div>-->
          <!--          </div>-->
          <div class="data">
            <template v-if="detail.videoIds && detail.videoIds.split(',').length > 0">
              <div v-for="item in detail.videoIds.split(',')" :key="item">
                <video :src="view + item" controls />
              </div>
            </template>
            <template v-if="detail.imgIds && detail.imgIds.split(',').length > 0">
              <div v-for="item in detail.imgIds.split(',')" :key="item">
                <el-image :src="view + item" alt="" :preview-src-list="[view + item]" />
              </div>
            </template>
            <p v-if="detail.worksRemark" class="news-text" /><article v-html="detail.worksRemark" />
            </p>
          </div>

          <section
            v-if="detail.commentState && detail.commentState !== 0"
            class="common-container"
          >
            <h4>评论</h4>
            <section ref="commentContent" class="comment-con">
              <section v-if="isNoData4comment">
                暂无评论
              </section>
              <ul v-else v-infinite-scroll="onLoad" :infinite-scroll-delay="1000">
                <li
                  v-for="(item, index) in commentData"
                  :key="index"
                  class="comment-item"
                >
                  <img v-if="item.userView" :src="view + item.userView" alt>
                  <img v-else src="../../images/ic_default_portrait.png" alt="">
                  <section class="comment-item-right">
                    <section class="comment-item-right-content">
                      <section class="comment-name etc">
                        {{ item.userIdName || "张三" }}
                      </section>
                    </section>
                    <section class="comment-content">
                      {{ item.commentContent }}
                    </section>
                    <section v-if="item.userImgs" class="comment-img">
                      <el-image
                        v-for="ite in item.userImgs ? item.userImgs.split(','): ''"
                        :key="ite"
                        :z-index="99999"
                        :src="view + it"
                        :preview-src-list="[view + it]"
                      >
                        <div slot="error" class="image-slot">
                          <i class="el-icon-picture-outline" />
                        </div>
                      </el-image>
                    </section>
                    <section class="comment-time">
                      {{ item.crtTime }}
                    </section>
                    <ul
                      v-if="
                        item.activitySonCommentVOList &&
                          item.activitySonCommentVOList.rows &&
                          item.activitySonCommentVOList.rows.length
                      "
                      class="reply-content"
                    >
                      <li
                        v-for="ite in item.activitySonCommentVOList.rows"
                        :key="ite.id"
                      >
                        <p class="reply-info">
                          <template>
                            <span
                              class="replyer"
                            >{{ ite.userIdName || "张五" }}<span>回复</span>{{ ite.commentParentIdName || "张四" }}：</span>
                          </template>
                          <span class="reply-info">{{ ite.commentContent }}</span>
                        </p>
                        <section v-if="ite.userImgs" class="reply-img">
                          <el-image
                            v-for="it in ite.userImgs ? ite.userImgs.split(','): ''"
                            :key="it"
                            :z-index="99999"
                            :src="view + it"
                            :preview-src-list="[view +it]"
                          >
                            <div slot="error" class="image-slot">
                              <i class="el-icon-picture-outline" />
                            </div>
                          </el-image>
                        </section>
                        <section class="reply-time">
                          {{ ite.crtTime }}
                        </section>
                      </li>
                      <section
                        v-if="item.activitySonCommentVOList.rows.length < item.activitySonCommentVOList.total"
                        class="get-son"
                        @click="
                          getMoreComment(
                            index,
                            item.activitySonCommentVOList.rows.length,
                            item.id
                          )
                        "
                      >
                        查看更多
                      </section>
                    </ul>
                  </section>
                </li>
              </ul>
            </section>
          </section>
        </div>
        <div class="menu">
          <div
            v-if="detail.favorState === 1 || detail.favorState === 2"
            class="menuItem"
          >
            <img
              class="icon"
              src="../../images/fabulous-a.png"
              alt
            >
            <span class="num">{{ evaluateInfo.favorNumber || 0 }}</span>
          </div>
          <div v-if="detail.favorState === 2" class="menuItem">
            <img
              class="icon"
              src="../../images/unfabulous-a.png"
              alt
            >
            <span class="num">{{ evaluateInfo.treadNumber || 0 }}</span>
          </div>
          <div v-if="detail.evaluateState === 1" class="menuItem">
            <img class="icon" src="../../images/score.png" alt>
            <span class="num">{{ evaluateInfo.evaluateScore || 0 }}分</span>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="handleCancel()">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, getRejectData, queryEvaluate, queryPage4comment, queryPage4commentSon } from './api'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'WorksPreview',
  directives: { elDragDialog },
  props: {
    id: {
      type: String,
      default: ''
    },
    worksType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      detail: {},
      commentData: [],
      state: true,
      previewLoading: false,
      view: '/API-DOCUMENT/document/file/download?id=',
      page: 1,
      pageSize: 5,
      total: 0,
      isNoData4comment: false,
      finished: false,
      evaluateInfo: {}
    }
  },
  created() {
    if (this.worksType === '3') {
      this.getRejectData()
    } else {
      this.getData()
      this.getCommendList()
      this.getEvaluate()
    }
  },
  methods: {
    /**
     * 根编辑回显
     */
    getRejectData() {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        getRejectData({ id: this.id }).then(res => {
          this.previewLoading = false
          this.detail = res.data.data
        }).catch(error => {
          this.previewLoading = false
          reject(error)
        })
      })
    },
    /**
     * 根编辑回显
     */
    getData() {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        get({ id: this.id }).then(res => {
          this.previewLoading = false
          this.detail = res.data.data
        }).catch(error => {
          this.previewLoading = false
          reject(error)
        })
      })
    },
    handleCancel() {
      this.$emit('closeSet')
    },
    /**
     * 获取评论列表
     */
    getCommendList() {
      return new Promise((resolve, reject) => {
        this.page = 1
        this.previewLoading = true
        queryPage4comment({
          page: this.page,
          pageSize: this.pageSize,
          worksId: this.id
        }).then(res => {
          this.previewLoading = false
          res = res.data
          if (res && res.code === '200') {
            this.commentData = res.data.rows || []
            this.total = res.data.total
            if (this.commentData.length < this.pageSize || this.commentData.length >= this.total) {
              if (this.commentData.length === 0) {
                this.isNoData4comment = true
              } else {
                this.isNoData4comment = false
              }
              this.finished = true
            } else {
              this.finished = false
            }
            this.page++
            resolve()
          } else {
            this.isNoData4comment = true
            reject()
          }
        }).catch(() => {
          this.isNoData4comment = true
          this.previewLoading = false
          reject()
        })
      })
    },
    onLoad() {
      if (this.finished) {
        // this.$message({
        //   title: '提示',
        //   message: '没有更多了~',
        //   type: 'warning',
        //   duration: 2000
        // })
        return
      }
      this.previewLoading = true
      queryPage4comment({
        page: this.page,
        pageSize: this.pageSize,
        worksId: this.id
      }).then(res => {
        this.previewLoading = false
        res = res.data
        if (res && res.code === '200') {
          const _list = res.data.rows || []
          this.commentData = [...this.commentData, ..._list]
          this.total = res.data.total || 0
          if (_list.length < this.pageSize || this.commentData.length >= this.total) {
            this.finished = true
            return
          }
          this.page++
        } else {
          this.error = true
        }
      }).catch(() => {
        this.previewLoading = false
        this.error = true
      })
    },
    /**
     * 获取二级评论列表
     */
    getMoreComment(pIndex, curNum, rootId) {
      this.previewLoading = true
      const page = Math.ceil(curNum / 5) + 1
      queryPage4commentSon({
        page: page,
        pageSize: 5,
        worksId: this.id,
        commentRootId: rootId
      }).then(res => {
        res = res.data
        this.previewLoading = false
        if (res && res.code === '200') {
          const _list = res.data.rows || []
          if (_list.length > 0) {
            _list.forEach(item => {
              this.commentData[pIndex].activitySonCommentVOList.rows.push(item)
            })
          }
        }
      }).catch(() => {
        this.previewLoading = false
      })
    },
    /**
     * 点赞、踩、评分相关数据
     */
    getEvaluate() {
      this.previewLoading = true
      return new Promise((resolve, reject) => {
        queryEvaluate({
          worksId: this.id
        }).then(res => {
          this.previewLoading = false
          if (res.data.code === '200') {
            this.evaluateInfo = res.data.data
            resolve()
          }
        }).catch(err => {
          this.previewLoading = false
          reject()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.works-preview {
  position: relative;

  .preview-container {
    height: 80vh;
    position: relative;

    .menu {
      position: absolute;
      right: 15px;
      bottom: 155px;
      width: 50px;
      height: 225px;
      z-index: 9;

      .menuItem {
        margin: 10px auto;
        width: 47px;
        height: 47px;
        border-radius: 50%;
        background: rgba(0, 0, 0, 0.75);
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        .icon {
          width: 16px;
          height: 16px;
          flex: 0 0 auto;
        }

        .num {
          font-size: 10px;
          font-family: PingFang SC Regular, PingFang SC Regular-Regular;
          font-weight: 400;
          color: #ffffff;
        }
      }
    }
  }

  .content {
    padding: 20px 0;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    overflow-y: auto;

    .title {
      font-size: 20px;
      font-family: PingFang SC Medium, PingFang SC Medium-Medium;
      font-weight: 700;
      text-align: left;
      color: #333333;
    }

    .time {
      margin-top: 15px;
      margin-bottom: 15px;
      font-size: 12px;
      font-family: PingFang SC Regular, PingFang SC Regular-Regular;
      font-weight: 400;
      text-align: left;
      color: #999999;
    }

    .data {
      margin-top: 7px;

      img, video {
        width: 100%;
      }
    }

    .checkInfo {
      background: #F8F8F8;
      padding: 15px 12px;
      font-size: 14px;
      font-family: PingFang SC Regular, PingFang SC Regular-Regular;
      font-weight: 400;

      .row {
        margin-bottom: 15px;
        display: flex;

        &:last-child {
          margin-bottom: 0;
        }

        .key {
          color: #999999;
          width: 80px;
        }

        .val {
          flex: 1;
          color: #333333;

          &.ytg {
            color: #7FC92E;
          }

          &.wtg {
            color: #EB523C;
          }

          &.shz {
            color: #42A7FD;
          }

          &.reason {
            font-weight: bold;
          }

        }
      }
    }

    .news-text {
      font-size: 15px;

      video, audio, img {
        display: block;
        max-width: 100% !important;
        width: auto;
        height: auto !important;
      }
    }

    .common-container {
      h4 {
        font-size: 14px;
        color: #333;
      }

      .comment-con {
        position: relative;
        min-height: 150px;

        ul {
          margin: 0;
          padding: 0;
          list-style: none;
        }

        .comment-item {
          display: flex;
          margin-bottom: 30px;
          width: 100%;

          &:last-child {
            margin-bottom: 10px;
          }

          & > img {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            flex: 0 0 auto;
          }

          .comment-item-right {
            flex: 1;

            .comment-item-right-content {
              display: flex;
              justify-content: space-between;

              .comment-name {
                font-size: 14px;
                font-family: PingFangSC-Regular;
                font-weight: bold;
                color: rgba(51, 51, 51, 1);
                line-height: 18px;
              }
            }

            .comment-time {
              display: flex;
              align-items: center;
              font-size: 12px;
              font-family: PingFangSC-Regular;
              font-weight: 400;
              color: #999;
              line-height: 18px;

              .reply {
                margin-left: 10px;
                color: #666;
              }
            }

            .comment-content {
              width: 293px;
              font-size: 14px;
              font-family: PingFangSC-Regular;
              font-weight: 400;
              color: rgba(51, 51, 51, 1);
              line-height: 21px;
              margin-top: 5px;
              word-wrap: break-word;
            }

            .comment-img {
              display: flex;
              margin: 5px 0;

              .el-image {
                margin-left: 5px;
                width: 84px;
                height: 84px;

                &:first-child {
                  margin-left: 0;
                }

                img {
                  width: 84px;
                  height: 84px;
                  border-radius: 6px;
                }
              }

            }

            .reply-content {
              width: 100%;
              background-color: #fafafa;
              padding: 10px 10px;
              margin-top: 5px;

              li {
                margin-bottom: 20px;
                position: relative;

                &:last-child {
                  margin-bottom: 0;

                  &:after {
                    display: none;
                  }
                }

                &:after {
                  left: 0;
                  right: 0;
                  width: auto;
                  height: 1px;
                  position: absolute;
                  z-index: 0;
                  bottom: -10px;
                  content: "";
                  background-image: -webkit-gradient(
                          linear,
                          left bottom,
                          left top,
                          color-stop(50%, #ececec),
                          color-stop(0, transparent)
                  );
                  background-image: linear-gradient(0deg, #ececec 50%, transparent 0);
                }
              }

              .reply-info {
                display: flex;
                align-items: center;
                flex-wrap: wrap;

                .replyer {
                  color: #9E9E9E;

                  span {
                    color: #666;
                  }
                }

                .reply-info {
                  color: #333;
                  font-weight: bold;
                }
              }

              .reply-img {
                display: flex;
                margin: 10px 0;
                justify-content: flex-start;

                .el-image {
                  margin-left: 5px;
                  width: 84px;
                  height: 84px;

                  &:first-child {
                    margin-left: 0;
                  }

                  img {
                    width: 84px;
                    height: 84px;
                    border-radius: 6px;
                  }
                }
              }

              .reply-time {
                color: #999;
                font-size: 12px;
                margin-top: 8px;

                .reply {
                  margin-left: 10px;
                  color: #666;
                }
              }

              .get-son {
                cursor: pointer;
                text-align: center;
                @include font-color($mainColor)
              }
            }
          }
        }
      }
    }
  }

  .el-image-viewer__wrapper {
    z-index: 9999 !important;
  }
}
</style>
